#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
栈（Stack）的Python实现
"""

class Stack:
    def __init__(self):
        self.items = []
    
    def push(self, item):
        """入栈"""
        self.items.append(item)
    
    def pop(self):
        """出栈"""
        if not self.is_empty():
            return self.items.pop()
        raise IndexError("栈为空")
    
    def peek(self):
        """查看栈顶元素"""
        if not self.is_empty():
            return self.items[-1]
        raise IndexError("栈为空")
    
    def is_empty(self):
        """判断栈是否为空"""
        return len(self.items) == 0
    
    def size(self):
        """获取栈的大小"""
        return len(self.items)
    
    def __str__(self):
        return str(self.items)


# 测试代码
if __name__ == "__main__":
    stack = Stack()
    
    # 基本操作测试
    print("入栈操作:")
    stack.push(1)
    stack.push(2)
    stack.push(3)
    print(f"栈内容: {stack}")
    
    print(f"栈顶元素: {stack.peek()}")
    print(f"出栈元素: {stack.pop()}")
    print(f"栈内容: {stack}")
    print(f"栈大小: {stack.size()}")
